package in.muton.edushare.auth;

import javax.servlet.http.Cookie;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.url.URLLoader;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.wicket.RequestCycle;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.protocol.http.WebRequest;
import org.apache.wicket.protocol.http.WebResponse;

import com.edushare.common.constants.URLConstants;
import com.edushare.common.util.CookieUtils;
import com.edushare.common.vo.Users;

public class SessionClashResolverPage extends WebPage {
	
	private static Log log = LogFactory.getLog(SessionClashResolverPage.class);

	private Users conflictingUser;
	private Users loggedInUser;
	public SessionClashResolverPage() {
		log.debug("Session Resolver Page.");
		EduShareSession.get().bind();
		conflictingUser = EduShareSession.get().getConflictingUser();
		loggedInUser =  EduShareSession.get().getLoggedInUser();
		log.debug("\ncoflicting: "+conflictingUser+"\nLoggedin: "+ loggedInUser);
		if(conflictingUser==null || loggedInUser==null){
			EduShareSession.get().invalidate();
			setResponsePage(URLLoader.getPageClass(URLConstants.LOGIN_PATH));
		}
		
		final Form<Void> form = new Form<Void>("suForm");
		add(form);
		
		final Label oldusername = new Label("oldUserName", loggedInUser.getUserName());
		form.add(oldusername);
		
		final Button switchToNew = new Button("switch"){

			private static final long serialVersionUID = 495313354265579292L;

			@Override
			public void onSubmit() {
				log.debug("replacing current session with new one");
				log.debug("Before Invalidating:: hash: "+EduShareSession.get().hashCode()+", toString:" +EduShareSession.get().toString());
				Cookie[] cookies = ((WebRequest)getRequestCycle().getRequest()).getCookies();
				for(Cookie c: cookies){
					if(c.getName().equalsIgnoreCase(CookieUtils.COOKIE_KEY_USER_NAME))
						((WebResponse)RequestCycle.get().getResponse()).clearCookie(c);
				}
				Cookie cookie = CookieUtils.createUserNameCookie(conflictingUser);
				cookie.setPath("/");
				((WebResponse)RequestCycle.get().getResponse()).addCookie(cookie);
//				((WebResponse)RequestCycle.get().getResponse()).addCookie(new Coo)
				EduShareSession.get().invalidateNow();
				log.info("Switching from "+loggedInUser.getUserName()+"["+loggedInUser.getUserId()+"], " +
						"to "+conflictingUser.getUserName()+"["+ conflictingUser.getUserId()+"].");

				
				setResponsePage(LoginUsingCookie.class);
				super.onSubmit();
			}
		};
		
		form.add(switchToNew);
		
		final Button dontSwitch = new Button("dontSwitch"){

			private static final long serialVersionUID = 4983118350065172319L;
			
			@Override
			public void onSubmit() {
				log.debug("Invalidating conflicting user.");
				EduShareSession.get().invalidateConflictingUser();
				setResponsePage(URLLoader.getPageClass(URLConstants.USER_PROFILE_PATH));
				super.onSubmit();
			}
			
		};
		
		form.add(dontSwitch);
		
	}

}
